//
//  TKIMAlbumGroupViewController.m
//  TKPluginDemo
//
//  Created by liupm on 16/1/12.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMAlbumGroupViewController.h"
#import "TKIMAlbumViewCell.h"
#import "TKIMPhotoSelectHelper.h"
#import "TKIMImageSelectViewController.h"
@interface TKIMAlbumGroupViewController ()
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) TKIMPhotoSelectHelper *hepler;
@end

@implementation TKIMAlbumGroupViewController

-(void)loadView
{
    [super loadView];
    self.navigationItem.title = @"相册";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    self.tableView.rowHeight = 80;
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)]];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 70, 0, 0)];
    //获得相册组
    [self getAlbumGroups];
}


/**
 *  @Author lpm, 16-04-21 16:04:51
 *
 *  @brief  获得相册组
 */
-(void)getAlbumGroups
{
    //获得相册组
    [self.hepler getAlbumGroups:^(NSMutableArray *array)
     {
         if (array)
         {
             self.dataSource = array;
             [self.tableView reloadData];
             if (![TKIMImagePickerSet shareInstance].showAblumList)
             {
                 TKIMImageSelectViewController *photoCtrl = [[TKIMImageSelectViewController alloc] init];
                 for (TKIMAlbumModel *model in self.dataSource)
                 {
                     if ([model.albumName isEqualToString:@"相机胶卷"])
                     {
                         photoCtrl.model = model;
                         [self.navigationController pushViewController:photoCtrl animated:NO];
                         break;
                     }
                 }
             }
         }
     }];
}


/**
 *  @Author lpm, 16-04-21 16:04:33
 *
 *  @brief  关闭当前控制器
 */
-(void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Table view data source
/**
 *  @Author lpm, 16-01-12 11:01:34
 *
 *  @brief
 *
 *  @param tableView
 *
 *  @return
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


/**
 *  @Author lpm, 16-01-12 11:01:38
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}


/**
 *  @Author lpm, 16-01-12 11:01:43
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 *
 *  @return
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TKIMAlbumViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[TKIMAlbumViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    
    TKIMAlbumModel *model = self.dataSource[indexPath.row];
    cell.album = model;
    return cell;
}


/**
 *  @Author lpm, 16-01-12 13:01:59
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TKIMImageSelectViewController *photoCtrl = [[TKIMImageSelectViewController alloc] init];
    photoCtrl.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:photoCtrl animated:YES];
}

#pragma mark  属性

-(TKIMPhotoSelectHelper *)hepler
{
    if (!_hepler)
    {
        _hepler = [[TKIMPhotoSelectHelper alloc] init];
    }
    return _hepler;
}


-(NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


@end
